//
//  HYDateCitySelectView.m
//  Booking
//
//  Created by Duntech on 2018/4/20.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYDateCitySelectView.h"
#import <PGDatePickManager.h>
#import "HYCityModel.h"
#import "HYBuildingModel.h"
#import "ZJUsefulPickerView.h"


@interface HYDateCitySelectView() <PGDatePickerDelegate>

@property (nonatomic,strong) UIView *blackBgView;
@property (nonatomic,strong) UIView *selectBgView;
@property (nonatomic,strong) UIButton *dateBtn;
@property (nonatomic,strong) UIView *timeBgView;
@property (nonatomic,strong) UIView *topLineView;
@property (nonatomic,strong) UIView *bottomLineView;
@property (nonatomic,strong) UIButton *startTimeBtn;
@property (nonatomic,strong) UIButton *endTimeBtn;
@property (nonatomic,strong) UIButton *cityBtn;
@property (nonatomic,strong) UIButton *addressBtn;
@property (nonatomic,strong) UIButton *searchBtn;
@property (nonatomic,strong) UIImageView *calendarImgView;
@property (nonatomic,strong) UIImageView *clockImgView;
@property (nonatomic,strong) UIImageView *cityImgView;
@property (nonatomic,strong) UIImageView *buildingImgView;

@property (nonatomic,assign) NSInteger lastSelectIndex;
@property (nonatomic,strong) NSDate *selectDate;
@property (nonatomic,copy)   NSString *selectStartTime;
@property (nonatomic,copy)   NSString *selectEndTime;
@property (nonatomic,copy)   NSString *selecCityID;
@property (nonatomic,copy)   NSString *selectBuildingID;
@property (nonatomic,copy)   NSString *selectCity;
@property (nonatomic,copy)   NSString *selectBuilding;

@property (nonatomic, strong) NSMutableArray *cityBuildsMArr;

@end


@implementation HYDateCitySelectView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupSubviews];
        [self setupMasonryLayout];
        self.backgroundColor = KAPP_Clear_COLOR;
    }
    return self;
}

- (void)setupSubviews{
    
    [self addSubview:self.blackBgView];
    [self addSubview:self.selectBgView];
    [self.selectBgView addSubview:self.timeBgView];
    [self.selectBgView addSubview:self.dateBtn];
    [self.selectBgView addSubview:self.startTimeBtn];
    [self.selectBgView addSubview:self.endTimeBtn];
    [self.selectBgView addSubview:self.cityBtn];
    [self.selectBgView addSubview:self.addressBtn];
    [self.selectBgView addSubview:self.searchBtn];
    
    [self.selectBgView addSubview:self.calendarImgView];
    [self.selectBgView addSubview:self.clockImgView];
    [self.selectBgView addSubview:self.cityImgView];
    [self.selectBgView addSubview:self.buildingImgView];
    
    [self.selectBgView addSubview:self.topLineView];
    [self.selectBgView addSubview:self.bottomLineView];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
}

- (void)setupMasonryLayout{
    
    [_blackBgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self);
    }];
    
    _selectBgView.frame = CGRectMake(0, 0, KSCREEN_WIDTH, 0);
    
    [_dateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self).offset(10 * WIDTH_MULTIPLE);
        make.right.equalTo(self).offset(-10 * WIDTH_MULTIPLE);
        make.height.mas_equalTo(40 * WIDTH_MULTIPLE);
    }];
    
    [_calendarImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.dateBtn).offset(12 * WIDTH_MULTIPLE);
        make.bottom.equalTo(self.dateBtn).offset(-12 * WIDTH_MULTIPLE);
    }];
    
    [_startTimeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.dateBtn);
        make.top.equalTo(self.dateBtn.mas_bottom).offset(10 * WIDTH_MULTIPLE);
        make.height.mas_equalTo(60 * WIDTH_MULTIPLE);
        make.width.mas_equalTo((KSCREEN_WIDTH - 26 * WIDTH_MULTIPLE) / 2);
    }];
    
    [_clockImgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self);
        make.centerY.equalTo(self.startTimeBtn);
    }];
    
    [_endTimeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.startTimeBtn.mas_right).offset(6 * WIDTH_MULTIPLE);
        make.top.bottom.equalTo(self.startTimeBtn);
        make.right.equalTo(self.dateBtn);
    }];
    
    [_cityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(self.startTimeBtn);
        make.top.mas_equalTo(self.startTimeBtn.mas_bottom).offset(10 * WIDTH_MULTIPLE);
        make.height.mas_equalTo(40 * WIDTH_MULTIPLE);
    }];
    
    [_cityImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.cityBtn).offset(10 * WIDTH_MULTIPLE);
        make.bottom.equalTo(self.cityBtn).offset(-10 * WIDTH_MULTIPLE);
    }];
    
    [_addressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.cityBtn);
        make.left.right.equalTo(self.endTimeBtn);
    }];
    
    [_buildingImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.cityImgView);
        make.left.equalTo(self.addressBtn).offset(10 * WIDTH_MULTIPLE);
    }];
    
    [_searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.dateBtn);
        make.top.equalTo(self.cityBtn.mas_bottom).offset(20 * WIDTH_MULTIPLE);
        make.height.mas_equalTo(50 * WIDTH_MULTIPLE);
    }];
    
    [_timeBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.startTimeBtn.mas_top);
        make.left.mas_equalTo(self.startTimeBtn.mas_left);
        make.bottom.mas_equalTo(self.startTimeBtn.mas_bottom);
        make.right.mas_equalTo(self.endTimeBtn.mas_right);
    }];
    
    [_topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.timeBgView.mas_centerX);
        make.top.mas_equalTo(self.timeBgView.mas_top);
        make.bottom.mas_equalTo(self.clockImgView.mas_top).mas_offset(-5);
        make.width.mas_equalTo(1);
    }];
    
    [_bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.timeBgView.mas_centerX);
        make.bottom.mas_equalTo(self.timeBgView.mas_bottom);
        make.top.mas_equalTo(self.clockImgView.mas_bottom).mas_offset(5);
        make.width.mas_equalTo(1);
    }];
}

#pragma mark - public
- (void)showDateCitySelectViewInView:(UIView *)view{
    
    [view addSubview:self];
    self.frame = CGRectMake(0, KTopFilterViewHeight, KSCREEN_WIDTH, view.height - KTopFilterViewHeight);
    self.blackBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    [UIView animateWithDuration:0.2 animations:^{
       
        self.selectBgView.frame = CGRectMake(0, 0, self.width, 260 * WIDTH_MULTIPLE);
        for (UIView *subview in self.selectBgView.subviews) {
            
            subview.hidden = YES;
        }
    } completion:^(BOOL finished) {
        
        for (UIView *subview in self.selectBgView.subviews) {
            
            subview.hidden = NO;
        }
    }];
    
}

- (void)hideDateCitySelectView{
    
    [self removeFromSuperview];
}

-(void)initDefaultCityAndBuildingWithList:(NSArray *)cityBuildsList{
    HYCityModel *cityModel = cityBuildsList.count == 0 ? [HYCityModel new] : cityBuildsList[0];
    HYBuildModel *buildModel = cityModel.builds.count == 0 ? [HYBuildModel new] : cityModel.builds[0];
    self.selecCityID = cityModel.id;
    self.selectCity = cityModel.name ? cityModel.name : @"";
    self.selectBuildingID = buildModel.id;
    self.selectBuilding = buildModel.name ? buildModel.name : @"";
    [self.cityBtn setTitle:cityModel.name ? cityModel.name : NSLocalizedString(@"allStatus", nil)  forState:(UIControlStateNormal)];
    [self.addressBtn setTitle:buildModel.name ? buildModel.name : NSLocalizedString(@"allStatus", nil)  forState:(UIControlStateNormal)];
    
    [self.cityBuildsMArr removeAllObjects];
    [self.cityBuildsMArr addObjectsFromArray:cityBuildsList];
}

#pragma mark - action
- (void)selectCityAndBuildingAction{
    
    NSMutableArray *cityMArr = [NSMutableArray new];
    NSMutableDictionary *buildsMDic = [NSMutableDictionary new];
    for (HYCityModel *cityModel in self.cityBuildsMArr) {
        [cityMArr addObject:cityModel.name];
        NSMutableArray *buildsMArr = [NSMutableArray new];
        for (HYBuildModel *buildModel in cityModel.builds) {
            [buildsMArr addObject:buildModel.name];
        }
        [buildsMDic setValue:buildsMArr forKey:cityModel.name];
    }
    if (cityMArr.count == 0) {
        [JRToast showWithText:NSLocalizedString(@"noOptions", nil)];
        return;
    }
    NSArray *multipleAssociatedData = @[cityMArr,buildsMDic];
    kWeakSelf(weakSelf);
    [ZJUsefulPickerView showMultipleAssociatedColPickerWithToolBarText:NSLocalizedString(@"selectCity", nil) withDefaultValues:nil withData:multipleAssociatedData withCancelHandler:nil withDoneHandler:^(NSArray *selectedValues) {
        NSLog(@"%@",selectedValues);
        int cityIndex = (int)[cityMArr indexOfObject:selectedValues[0]];
        HYCityModel *cityModel = weakSelf.cityBuildsMArr[cityIndex];
        weakSelf.selectCity = cityModel.name;
        weakSelf.selecCityID = cityModel.id;
        [weakSelf.cityBtn setTitle:cityModel.name forState:(UIControlStateNormal)];
        
        
        if ([selectedValues[1] isEqualToString:@""]) {
            weakSelf.selectBuilding = @"";
            weakSelf.selectBuildingID = nil;
            [weakSelf.addressBtn setTitle:NSLocalizedString(@"allStatus", nil) forState:(UIControlStateNormal)];
        }
        else{
            
            NSArray *buildsArr = [buildsMDic objectForKey:selectedValues[0]];
            int buildIndex = (int)[buildsArr indexOfObject:selectedValues[1]];
            HYBuildModel *buildModel = cityModel.builds[buildIndex];
            weakSelf.selectBuilding = buildModel.name;
            weakSelf.selectBuildingID = buildModel.id;
            [weakSelf.addressBtn setTitle:buildModel.name forState:(UIControlStateNormal)];
        }
    }];
}

- (void)dateBtnAction{
    
    self.lastSelectIndex = 1;
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc] init];
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerMode = PGDatePickerModeDate;
    datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:15 * 60];
//    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    datePickManager.isShadeBackgroud = YES;
    [self.viewController presentViewController:datePickManager animated:false completion:nil];
}

- (void)startTimeBtnAction{
    
    self.lastSelectIndex = 2;
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc] init];
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerMode = PGDatePickerModeTime;
    if ([HYDateHandle isSameWithDateA:self.selectDate dataB:[NSDate date] formater:@"yyyy-MM-dd"]) {
        datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:15 * 60];
    }
    else{
        NSDate *sinceDate = [NSDate dateWithString:[HYDateHandle getCurrentDateStrYYYYMMDDWithTimeInterval:0] format:@"yyyy-MM-dd"];
        datePicker.minimumDate = [NSDate dateWithTimeInterval:15 * 60 sinceDate:sinceDate];
    }
    datePickManager.isShadeBackgroud = YES;
    [self.viewController presentViewController:datePickManager animated:false completion:nil];
    
}

- (void)endTimeBtnAction{
    
    self.lastSelectIndex = 3;
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc] init];
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerMode = PGDatePickerModeTime;
    if ([HYDateHandle isSameWithDateA:self.selectDate dataB:[NSDate date] formater:@"yyyy-MM-dd"]) {
        datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:45 * 60];
    }
    else{
        NSDate *sinceDate = [NSDate dateWithString:[HYDateHandle getCurrentDateStrYYYYMMDDWithTimeInterval:0] format:@"yyyy-MM-dd"];
        datePicker.minimumDate = [NSDate dateWithTimeInterval:45 * 60 sinceDate:sinceDate];
    }
    datePickManager.isShadeBackgroud = YES;
    [self.viewController presentViewController:datePickManager animated:false completion:nil];
}

- (void)searchBtnAction{
    
    NSDate *startDate = [NSDate dateWithString:self.selectStartTime format:@"HH:mm:ss"];
    NSInteger startTimestamp = [startDate timeIntervalSince1970];
    NSDate *endDate = [NSDate dateWithString:self.selectEndTime format:@"HH:mm:ss"];
    NSInteger endTimeStamp = [endDate timeIntervalSince1970];
    if (endTimeStamp - startTimestamp < minMeetingTimeDifference) {
        
        [JRToast showWithText:NSLocalizedString(@"startEndTimeTips", nil) duration:2];
        return;
    }
    if (endTimeStamp - startTimestamp > maxMeetingTimeDifference) {
        
        [JRToast showWithText:NSLocalizedString(@"meetingIn4HourTips", nil) duration:2];
        return;
    }
    
    [self hideDateCitySelectView];
    if (self.dateCitySearchBlock) {
        
        self.dateCitySearchBlock(self.selectDate, self.selectStartTime, self.selectEndTime, self.selecCityID, self.selectBuildingID,self.selectCity,self.selectBuilding);
    }
}

#pragma mark - PGDateSelectDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
    NSLog(@"dateComponents = %@", dateComponents);
    
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *selectDate = [gregorianCalendar dateFromComponents:dateComponents];
//    NSInteger weekday = [gregorianCalendar component:NSCalendarUnitWeekday fromDate:selectDate];
    DLog(@"selectDate : %@",selectDate);
    
    if (self.lastSelectIndex == 1) {

        self.selectDate = selectDate;
        NSString *weekStr = [HYDateHandle weekStrFromDate:selectDate];
        NSString *dateStr = [selectDate stringWithFormat:@"yyyy-MM-dd"];
        [_dateBtn setTitle:[NSString stringWithFormat:@"%@，%@",weekStr,dateStr] forState:UIControlStateNormal];
    }
    else if (self.lastSelectIndex == 2){
        
        NSString *dateStr = [NSString stringWithFormat:@"%@\n%02ld:%02ld",NSLocalizedString(@"startTime", nil),(long)dateComponents.hour,(long)dateComponents.minute];
        self.selectStartTime = [NSString stringWithFormat:@"%@:00",[HYDateHandle getDateStringWithDate:selectDate formater:@"HH:mm"]];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:dateStr attributes:@{NSFontAttributeName : KFitFont(16), NSForegroundColorAttributeName : KAPP_BLACK_COLOR}];
        NSRange range = [dateStr rangeOfString:NSLocalizedString(@"startTime", nil)];
        [attributedStr addAttributes:@{NSForegroundColorAttributeName : KAPP_ORANGE_COLOR, NSFontAttributeName : KFitFont(14)} range:range];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        [style setLineSpacing:10];
        [attributedStr addAttributes:@{NSParagraphStyleAttributeName : style} range:NSMakeRange(0, attributedStr.length)];
        [_startTimeBtn setAttributedTitle:attributedStr forState:UIControlStateNormal];

    }
    else if (self.lastSelectIndex == 3){
        

        NSString *dateStr = [NSString stringWithFormat:@"%@\n%02ld:%02ld",NSLocalizedString(@"endTime", nil),(long)dateComponents.hour,(long)dateComponents.minute];
        self.selectEndTime = [NSString stringWithFormat:@"%@:00",[HYDateHandle getDateStringWithDate:selectDate formater:@"HH:mm"]];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:dateStr attributes:@{NSFontAttributeName : KFitFont(16), NSForegroundColorAttributeName : KAPP_BLACK_COLOR}];
        NSRange range = [dateStr rangeOfString:NSLocalizedString(@"endTime", nil)];
        [attributedStr addAttributes:@{NSForegroundColorAttributeName : KAPP_ORANGE_COLOR, NSFontAttributeName : KFitFont(14)} range:range];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        [style setLineSpacing:10];
        [attributedStr addAttributes:@{NSParagraphStyleAttributeName : style} range:NSMakeRange(0, attributedStr.length)];
        [_endTimeBtn setAttributedTitle:attributedStr forState:UIControlStateNormal];
    }
}

#pragma mark - lazyload
- (UIView *)blackBgView{
    
    if (!_blackBgView) {
        _blackBgView = [UIView new];
        _blackBgView.backgroundColor = KAPP_BLACK_COLOR;
        _blackBgView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            
            CGPoint tapPoint = [sender locationInView:self];
            if (!CGRectContainsPoint(self.selectBgView.frame, tapPoint)) {
                [self hideDateCitySelectView];
            }
            
        }];
        [_blackBgView addGestureRecognizer:tapGes];
    }
    return _blackBgView;
}

- (UIView *)selectBgView{
    
    if (!_selectBgView) {
        
        _selectBgView = [UIView new];
        _selectBgView.backgroundColor = KCOLOR(@"e1e1e1");
    }
    return _selectBgView;
}

- (UIButton *)dateBtn{
    
    if (!_dateBtn) {
        _dateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _dateBtn.titleLabel.font = KFitFont(16);
        _dateBtn.backgroundColor = KAPP_WHITE_COLOR;
        _dateBtn.layerCornerRadius = 5;
        [_dateBtn setTitleColor:KAPP_BLACK_COLOR forState:UIControlStateNormal];
        
        NSDate *date = [NSDate date]; // 获得时间对象
        //英文系统时间计算方式
//        NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
//        NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
//        NSDate *currentDate = [date dateByAddingTimeInterval:time];// 然后把差的时间加上,就是当前系统准确的时间
        
//        NSArray *weekArr = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
//        NSArray *weekArr = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday"];
//        NSInteger week = date.weekday;
        NSString *weekStr = [HYDateHandle weekStrFromDate:date];
        NSDateFormatter *dateFormatter = [NSDateFormatter new];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *dateStr = [dateFormatter stringFromDate:date];
        NSString *str = [NSString stringWithFormat:@"%@，%@",weekStr,dateStr];
        [_dateBtn setTitle:str forState:UIControlStateNormal];
        [_dateBtn addTarget:self action:@selector(dateBtnAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _dateBtn;
}

- (UIButton *)startTimeBtn{
    
    if (!_startTimeBtn) {
        
        _startTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _startTimeBtn.backgroundColor = KAPP_WHITE_COLOR;
        _startTimeBtn.layerCornerRadius = 5;
        _startTimeBtn.titleLabel.numberOfLines = 0;
        NSString *startTimeStr = [NSString stringWithFormat:@"%@\n%@",NSLocalizedString(@"startTime", nil),[HYDateHandle getCurrentDateStrHHmmWithTimeInterval:0]];
        self.selectStartTime = [NSString stringWithFormat:@"%@:00",[HYDateHandle getCurrentDateStrHHmmWithTimeInterval:0]];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:startTimeStr attributes:@{NSFontAttributeName : KFitFont(16), NSForegroundColorAttributeName : KAPP_BLACK_COLOR}];
        NSRange range = [startTimeStr rangeOfString:NSLocalizedString(@"startTime", nil)];
        [attributedStr addAttributes:@{NSForegroundColorAttributeName : KAPP_ORANGE_COLOR, NSFontAttributeName : KFitFont(14)} range:range];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        [style setLineSpacing:10];
        style.alignment = NSTextAlignmentCenter;
        [attributedStr addAttributes:@{NSParagraphStyleAttributeName : style} range:NSMakeRange(0, attributedStr.length)];
        [_startTimeBtn setAttributedTitle:attributedStr forState:UIControlStateNormal];
        _startTimeBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_startTimeBtn addTarget:self action:@selector(startTimeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startTimeBtn;
}

- (UIButton *)endTimeBtn{
    
    if (!_endTimeBtn) {
        
        _endTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _endTimeBtn.backgroundColor = KAPP_WHITE_COLOR;
        _endTimeBtn.layerCornerRadius = 5;
        _endTimeBtn.titleLabel.numberOfLines = 0;
        _endTimeBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        NSString *endDateStr = [NSString stringWithFormat:@"%@\n%@",NSLocalizedString(@"endTime", nil),[HYDateHandle getCurrentDateStrHHmmWithTimeInterval:60 * 60]];
        self.selectEndTime = [NSString stringWithFormat:@"%@:00",[HYDateHandle getCurrentDateStrHHmmWithTimeInterval:60 * 60]];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:endDateStr attributes:@{NSFontAttributeName : KFitFont(16), NSForegroundColorAttributeName : KAPP_BLACK_COLOR}];
        NSRange range = [endDateStr rangeOfString:NSLocalizedString(@"endTime", nil)];
        [attributedStr addAttributes:@{NSForegroundColorAttributeName : KAPP_ORANGE_COLOR, NSFontAttributeName : KFitFont(14)} range:range];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        [style setLineSpacing:10];
        style.alignment = NSTextAlignmentCenter;
        [attributedStr addAttributes:@{NSParagraphStyleAttributeName : style} range:NSMakeRange(0, attributedStr.length)];
        [_endTimeBtn setAttributedTitle:attributedStr forState:UIControlStateNormal];
        
        _endTimeBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_endTimeBtn addTarget:self action:@selector(endTimeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _endTimeBtn;
}

- (UIButton *)cityBtn{
    
    if (!_cityBtn) {
        
        _cityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cityBtn.backgroundColor = KAPP_WHITE_COLOR;
        _cityBtn.layerCornerRadius = 5;
        [_cityBtn setTitle:@"城市" forState:UIControlStateNormal];
        [_cityBtn setTitleColor:KAPP_BLACK_COLOR forState:UIControlStateNormal];
        _cityBtn.titleLabel.font = KFitFont(16);
        [_cityBtn addTarget:self action:@selector(selectCityAndBuildingAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_cityBtn setImage:[UIImage imageNamed:@"building"] forState:UIControlStateNormal];

    }
    return _cityBtn;
}

- (UIButton *)addressBtn{
    
    if (!_addressBtn) {
        
        _addressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addressBtn.backgroundColor = KAPP_WHITE_COLOR;
        _addressBtn.layerCornerRadius = 5;
        [_addressBtn setTitle:@"建筑" forState:UIControlStateNormal];
        [_addressBtn setTitleColor:KAPP_BLACK_COLOR forState:UIControlStateNormal];
        _addressBtn.titleLabel.font = KFitFont(16);
        [_addressBtn addTarget:self action:@selector(selectCityAndBuildingAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _addressBtn;
}

- (UIButton *)searchBtn{
    
    if (!_searchBtn) {
        
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchBtn.backgroundColor = KAPP_FILTER_TOP_COLOR;
        _searchBtn.layerCornerRadius = 5;
        [_searchBtn setTitle:NSLocalizedString(@"search", nil) forState:UIControlStateNormal];
        [_searchBtn setTitleColor:KAPP_WHITE_COLOR forState:UIControlStateNormal];
        _searchBtn.titleLabel.font = KFitFont(18);
        [_searchBtn addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

- (UIImageView *)calendarImgView{
    
    if (!_calendarImgView) {
        
        _calendarImgView = [UIImageView new];
        _calendarImgView.image = [UIImage imageNamed:@"calendar"];
        _calendarImgView.clipsToBounds = YES;
        _calendarImgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _calendarImgView;
}

- (UIImageView *)clockImgView{
    
    if (!_clockImgView) {
        
        _clockImgView = [UIImageView new];
        _clockImgView.image = [UIImage imageNamed:@"clock"];
        _clockImgView.clipsToBounds = YES;
        _clockImgView.backgroundColor = KAPP_WHITE_COLOR;
        _clockImgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _clockImgView;
}

- (UIImageView *)cityImgView{
    
    if (!_cityImgView) {
        
        _cityImgView = [UIImageView new];
        _cityImgView.image = [UIImage imageNamed:@"room"];
        _cityImgView.clipsToBounds = YES;
        _cityImgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _cityImgView;
}

- (UIImageView *)buildingImgView{
    
    if (!_buildingImgView) {
        
        _buildingImgView = [UIImageView new];
        _buildingImgView.image = [UIImage imageNamed:@"room"];
        _buildingImgView.clipsToBounds = YES;
        _buildingImgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _buildingImgView;
}

-(NSMutableArray *)cityBuildsMArr{
    if (!_cityBuildsMArr) {
        _cityBuildsMArr = [NSMutableArray new];
    }
    return _cityBuildsMArr;
}

-(NSDate *)selectDate{
    if (!_selectDate) {
        _selectDate = [NSDate date];
    }
    return _selectDate;
}

-(UIView *)timeBgView {
    if (!_timeBgView) {
        _timeBgView = [[UIView alloc]init];
        _timeBgView.backgroundColor = KAPP_WHITE_COLOR;
        _timeBgView.layerCornerRadius = 5;
    }
    return _timeBgView;
}

-(UIView *)topLineView{
    if (!_topLineView) {
        _topLineView = [UIView new];
        _topLineView.backgroundColor = RGBColor(238, 238, 238);
    }
    return _topLineView;
}

-(UIView *)bottomLineView{
    if (!_bottomLineView) {
        _bottomLineView = [UIView new];
        _bottomLineView.backgroundColor = RGBColor(238, 238, 238);
    }
    return _bottomLineView;
}

@end
